define(function (require, exports, module) {
    var $ = require("jquery");
    var angular = require('angular');
    var expanderModule = angular.module('myNav', [])
    expanderModule.directive('onFinishRender', function ($timeout) {
        /**
         * repeat加载完成触发事件
         */
        return {
            restrict: 'A',
            link: function (scope, element, attr) {
                if (scope.$last === true) {
                    $timeout(function () {
                        scope.$emit('ngRepeatFinished');
                    });
                }
            }
        }
    })
    expanderModule.directive('myNav', ['$parse', function ($parse) {
        return {
            restrict: 'EA',
            replace: true,
            transclude: true,
            templateUrl: '/public/js/myservice/common/nav.html',
            scope: {
                title: '@',
                textField: '@',
                navItems: "=",
                onchanged: "&",
                needMore: '@',
                ngModel: '='
            },
            link: function ($scope, element, attrs) {
                $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
                    /**
                     * repeat加载完成触发事件
                     */
                    setTimeout(function () {
                        if ($(element).find('.nav-filter-list').height() < 90) {
                            $scope.needMore = "false";
                            $scope.$applyAsync();
                        } else {
                            $scope.needMore = "true";
                            $scope.$applyAsync();
                        }
                    }, 0);
                });

                $scope.click = function (item) {
                    $.each($scope.navItems, function (i, o) {
                        o.selected = false;
                    })
                    item.selected = true;
                    if ($scope.ngModel) {
                        $scope.ngModel = item;
                    }

                    setTimeout(function () {
                        var call = $parse($scope.onchanged)
                        $scope.$apply(function () {
                            call() && call()(item);
                        });

                    }, 0);
                };

                $scope.showmore = function (expanded) {
                    $scope.expanded = expanded;
                };
            }
        }
    }]);

});